from zhdate import ZhDate
from pydantic import BaseModel

class Is_lunar_model(BaseModel):
    year: int
    month: int
    day: int

class Lunar_filter_model(BaseModel):
    date_list: list

def is_lunar(data):
    result = {"lunar": True, "error": ""}
    try: ZhDate(data.year, data.month, data.day)
    except Exception as e:
        result["lunar"] = False
        result["error"] = str(e.args[0])
        return result
    return result

def lunar_filter(data):
    result = {"date_list": [], "error": ""}
    try:
        date_list = data.date_list
        today = ZhDate.today()
        for i in date_list:
            s_date = i["date"].split("-")
            year = int(s_date[0])
            month = int(s_date[1])
            day = int(s_date[2])
            if "每年" == i["frequency"]:
                year = today.lunar_year
            elif "每月" == i["frequency"]:
                year = today.lunar_year
                month = today.lunar_month
            else:
                raise Exception("不存在的提醒频率: " + str(i["frequency"]))
            i_date = ZhDate(year, month, day)
            front = int(i["front"])
            back = int(i["back"])
            if 0 == i_date - today:
                i["prompt"] = "就在今天"
                result["date_list"].append(i)
            elif 0 < today - i_date <= back:
                i["prompt"] = f"已过{today - i_date}天"
                result["date_list"].append(i)
            elif 0 < i_date - today <= front:
                i["prompt"] = f"还有{i_date - today}天"
                result["date_list"].append(i)
    except Exception as e:
        result["error"] = str(e.args[0])
        return result
    return result

def cur_lunar():
    result = {"today": {}, "error": ""}
    try:
        today = ZhDate.today()
        result["today"]["year"] = today.lunar_year
        result["today"]["month"] = today.lunar_month
        result["today"]["day"] = today.lunar_day
    except Exception as e:
        result["error"] = str(e.args[0])
        return result
    return result

